package com.ljf.framework.Async;

import com.ljf.framework.LjfManager;
import com.ljf.framework.context.LjfContext;
import com.ljf.framework.context.LjfContextManager;
import com.ljf.framework.util.ObjectUtil;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 说明：
 *
 * @Auther: lijinfeng
 * @Date: 2024/8/2
 */
public abstract class AsyncAbstractService implements AsyncService {

    abstract <T>  AsyncResult<T> supplyAsyncCus(AsyncEvent<T> asyncEvent,ThreadPoolExecutor threadPoolExecutor);

    abstract void runAsyncCus(AsyncEvent asyncEvent,ThreadPoolExecutor threadPoolExecutor);

    @Override
    public <T> AsyncResult<T> supplyAsync(AsyncEvent<T> asyncEvent, ThreadPoolExecutor threadPoolExecutor) {

        asyncEvent.setLjfContext(LjfContextManager.getContext());

        if (ObjectUtil.isEmpty(threadPoolExecutor)){
            threadPoolExecutor = ThreadPoolConfiguration.getSystemPoolExecutorService();
        }
        return this.supplyAsyncCus(asyncEvent,threadPoolExecutor);
    }

    @Override
    public void runAsync(AsyncEvent asyncEvent, ThreadPoolExecutor threadPoolExecutor) {

        asyncEvent.setLjfContext(LjfContextManager.getContext());

        if (ObjectUtil.isEmpty(threadPoolExecutor)) {
            threadPoolExecutor = ThreadPoolConfiguration.getSystemPoolExecutorService();
        }
        this.runAsyncCus(asyncEvent,threadPoolExecutor);
    }
}
